﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using JasRibbonApplicatie.Classes;
using System.ComponentModel;
using System.Windows.Media.Animation;

namespace JasRibbonApplicatie.UserControls
{
    /// <summary>
    /// Interaction logic for ontleningDataList.xaml
    /// </summary>
    public partial class OntleningDataList : UserControl
    {
        private List<OntleningData> items;
        public List<OntleningData> ItemSource
        {
            get {
                return items;
            }
            set
            {
                items = value;
                if (items != null) RefreshList();
            }
        }
        private static int nrOfItems = 0;
        


		public OntleningDataList()
        {
            InitializeComponent();
        }
        public OntleningDataList(List<OntleningData> itemSource = null)
        {
            InitializeComponent();
            ItemSource = itemSource;
        }

        public void RefreshList()
        {
            contentGrid.Children.Clear();
            contentGrid.RowDefinitions.Clear();
            int i = 0;
            if (items != null)
            {
                foreach (var item in items)
                {
                    contentGrid.RowDefinitions.Add(new RowDefinition());
                    Grid gridItem = generateItem(item);
                    gridItem.SetValue(Grid.RowProperty, i);
                    contentGrid.Children.Add(gridItem);
                    i++;
                }
            }
        }

        private Grid generateItem(OntleningData obj)
        {
            ImageSourceConverter imgSConv = new ImageSourceConverter();

            //make a grid item
			Grid ontleningdataGrid = new Grid();
            ontleningdataGrid.VerticalAlignment = VerticalAlignment.Top;

            //set column definitions
            ontleningdataGrid.ColumnDefinitions.Add(new ColumnDefinition());
            ontleningdataGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(24) });
            ontleningdataGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(24) });

            //make name label
            Label itemNameLabel = new Label(){
                Content = obj.Name,
                HorizontalAlignment = HorizontalAlignment.Left
            };
            if (obj.type == typeof(ToebehorenType))
            {
                itemNameLabel.Padding = new Thickness(20, 5, 5, 5);
            }
            else
            {
                itemNameLabel.FontWeight = FontWeights.Bold;
            }
            ontleningdataGrid.Children.Add(itemNameLabel);

            //if need to be checkt add a tick image
            if (obj.Ontlening!=null && obj.Ontlening.magazijnierAfhaling != null)
            {
                Image buitenTickImage = new Image()
                {
                    Source = imgSConv.ConvertFromString("pack://application:,,,/JasRibbonApplicatie;component/Images/vinkjeGroen.png") as ImageSource,
                    Stretch = Stretch.Fill
                };
                buitenTickImage.SetValue(Grid.ColumnProperty, 1);
                ontleningdataGrid.Children.Add(buitenTickImage);
            }

            //if need to be checkt add a tick image
            if (obj.Ontlening != null && obj.Ontlening.magazijnierInlevering != null)
            {
                Image binnenTickImage = new Image()
                {
                    Source = imgSConv.ConvertFromString("/JasRibbonApplicatie;component/Images/vinkjeGroen.png") as ImageSource,
                    Stretch = Stretch.Fill
                };
                binnenTickImage.SetValue(Grid.ColumnProperty, 2);
                ontleningdataGrid.Children.Add(binnenTickImage);
            }
            nrOfItems++;
            setHoverEffect(ontleningdataGrid, "mybrush" + nrOfItems);
            
            //return the grid
            return ontleningdataGrid;
		}

        private void setHoverEffect(Grid mygrid, String brushname)
        {
            Storyboard mouseEnterStoryboard;
            Storyboard mouseLeaveStoryboard;
            SolidColorBrush myAnimatedBrush;

            #region init storyboard&brush
            myAnimatedBrush = new SolidColorBrush();
            myAnimatedBrush.Color = Color.FromScRgb(0, 0, 0, 0);

            this.RegisterName(brushname, myAnimatedBrush);
            ColorAnimation mouseEnterColorAnimation = new ColorAnimation();
            mouseEnterColorAnimation.To = Colors.Gray;
            mouseEnterColorAnimation.Duration = TimeSpan.FromSeconds(.5);
            Storyboard.SetTargetName(mouseEnterColorAnimation, brushname);
            Storyboard.SetTargetProperty(mouseEnterColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty));
            mouseEnterStoryboard = new Storyboard();
            mouseEnterStoryboard.Children.Add(mouseEnterColorAnimation);

            ColorAnimation mouseLeaveColorAnimation = new ColorAnimation();
            mouseLeaveColorAnimation.To = Color.FromScRgb(0, 0, 0, 0);
            mouseLeaveColorAnimation.Duration = TimeSpan.FromSeconds(.5);
            Storyboard.SetTargetName(mouseLeaveColorAnimation, brushname);
            Storyboard.SetTargetProperty(mouseLeaveColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty));
            mouseLeaveStoryboard = new Storyboard();
            mouseLeaveStoryboard.Children.Add(mouseLeaveColorAnimation);
            #endregion

            mygrid.Background = myAnimatedBrush;
            mygrid.MouseEnter += delegate(object sender, MouseEventArgs e)
            {
                mouseEnterStoryboard.Begin(this);
            };
            mygrid.MouseLeave += delegate(object sender, MouseEventArgs e)
            {
                mouseLeaveStoryboard.Begin(this);
            };
        }
    }
}
